// pages/detail/index.js
const app = getApp()
import {
  toast,
  distance
} from '../../utils/util'
import base from '../../api/base'
const WxParse = require('../../wxParse/wxParse.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorColor: 'rgba(0,0,0,0.4)',
    base: base.pro,
    info: {},
    imgUrls: [],
    infoTitle: '',
    infoObject: {
      companyAddress: {
        imgUrl: '../../assets/images/company_icon.png',
        content: ''
      },
      contact: {
        imgUrl: '../../assets/images/contact_icon.png',
        content: ''
      },
      address: {
        imgUrl: '../../assets/images/address_icon.png',
        content: ''
      },
      // updateTime: {
      //   imgUrl: '../../assets/images/update_icon.png',
      //   content: '2018年xxx更新'
      // }
    }, // 详情信息
    collectImg: '../../assets/images/collect.png',
    cancleImg: '../../assets/images/cancle_collect.png',
    collectFlag: false,
    equipment: {
      type: {
        content: '',
        title: '设备类型：'
      }
      // year: {
      //   content: '设备信息设备信息设备信息设备信息设备信息设备信息设备信息设备信息设备信息设备信息设备信息',
      //   title: '出场年份：'
      // },
      // introduce: {
      //   content: '设备信息设备信息设备信息设备信息设备信息设备信息设备信息设备信息设备信息设备信息设备信息设备信息',
      //   title: '设备介绍：'
      // }
    }, // 设备信息
    publicButton: '立即发布',
    tabIndex: 0,
    id: '',
    phone: '',
    collectid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.initId(options.id, options.tabIndex)
    this.getRemind()
  },
  initId(id, index) {
    this.setData({
      id,
      tabIndex: Number(index)
    })
    Number(index) === 0 ? this.getRentDetail() : Number(index) === 1 ? this.getSellDetail() : this.getMaintainDetail()
  },
  getRemind() {
    app.$api.index.findContent({
      code: 'traderClock'
    }).then((res) => {
      const data = res.data
      WxParse.wxParse('traderClock', 'html', data.content, this, 0)
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  getRentDetail() {
    let result = []
    const companyname = 'infoObject.companyAddress.content'
    const linkname = 'infoObject.contact.content'
    const detailedaddress = 'infoObject.address.content'
    const equname = 'equipment.type.content'
    const {
      id
    } = this.data
    app.$api.index.rentDetail({
      id
    }).then((res) => {
      const data = res.data
      result = result.concat(data.equimgurl.split(','))
      const info = this.calcDistance(data)
      const collectFlag = data.collectid? true: false
      console.log(collectFlag)
      this.setData({
        [companyname]: data.companyname,
        [linkname]: data.linkname,
        [detailedaddress]: data.detailedaddress,
        [equname]: data.equname,
        info: info,
        imgUrls: result,
        collectid: data.collectid,
        collectFlag
      })
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },
  getSellDetail() {
    let result = []
    const companyname = 'infoObject.companyAddress.content'
    const linkname = 'infoObject.contact.content'
    const detailedaddress = 'infoObject.address.content'
    const entscope = 'equipment.type.content'
    const {
      id
    } = this.data
    app.$api.index.saleDetail({
      id
    }).then((res) => {
      const data = res.data
      result = result.concat(data.imgurl.split(','))
      const info = this.calcDistance(data)
      const collectFlag = data.collectid? true: false
      this.setData({
        [companyname]: data.companyname,
        [linkname]: data.linkname,
        [detailedaddress]: data.detailedaddress,
        [entscope]: data.entscope,
        info: info,
        imgUrls: result,
        collectid: data.collectid,
        collectFlag
      })
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },
  getMaintainDetail() {
    let result = []
    const companyname = 'infoObject.companyAddress.content'
    const linkname = 'infoObject.contact.content'
    const detailedaddress = 'infoObject.address.content'
    const maintainscope = 'equipment.type.content'
    const {
      id
    } = this.data
    app.$api.index.maintainDetail({
      id
    }).then((res) => {
      const data = res.data
      result = result.concat(data.imgurl.split(','))
      const info = this.calcDistance(data)
      const collectFlag = data.collectid? true: false
      this.setData({
        [companyname]: data.companyname,
        [linkname]: data.linkname,
        [detailedaddress]: data.detailedaddress,
        [maintainscope]: data.maintainscope,
        info: info,
        imgUrls: result,
        collectid: data.collectid,
        collectFlag
      })
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const {
      tabIndex,
      id
    } = this.data
    // tabIndex === 0 ? this.rentDetail(id) : tabIndex === 1 ? this.sellDetail(id) : this.maintainDetail(id)
  },
  rentDetail(id) {
    app.$api.index.rentDetail({
        id
      })
      .then((res) => {
        const data = res.data
        this.initData(data)
      }).catch((res) => {
        toast('请稍后再试')
      })
  },
  sellDetail(id) {
    app.$api.index.saleDetail({
        id
      })
      .then((res) => {
        const data = res.data
        this.initData(data)
      }).catch((res) => {
        toast('请稍后再试')
      })
  },
  maintainDetail(id) {
    app.$api.index.maintainDetail({
        id
      })
      .then((res) => {
        const data = res.data
        this.initData(data)
      }).catch((res) => {
        toast('请稍后再试')
      })
  },
  initData(data) {
    const url = `${base.pro}/common/viewImage?imagepath=`
    const companyAddress = `indicatorColor[companyAddress][content]`
    const linkName = `indicatorColor[contact][content]`
    const phone = this.data.phone
    const detailedAddress = `indicatorColor[address][content]`
    const result = []
    this.setData({
      imgUrls: result.push(`${url}${data.imgUrlUrl}`),
      entProfile: data.entProfile,
      [companyAddress]: data.companyAddress,
      [linkName]: data.linkName,
      phone: data.phone,
      [detailedAddress]: data.detailedAddress
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindReport() {
    const inforid = this.data.id
    const infortype = Number(this.data.tabIndex) + 1
    const {
      info
    } = this.data
    wx.navigateTo({
      url: `../../personal/report/index?inforid=${inforid}&infortype=${infortype}&info=${JSON.stringify(info)}`,
    })
  },
  calcDistance(item) {
    const locationA = {
      lat: Number(wx.getStorageSync('location').lat),
      lng: Number(wx.getStorageSync('location').lng)
    }
    const locationB = {
      lat: Number(item.lat),
      lng: Number(item.lon)
    }
    const distanceValue = distance(locationA, locationB)
    item = Object.assign({}, item, {
      distance: distanceValue
    })
    return item
  },
  bindNavigation() {
    const {
      info
    } = this.data
    wx.openLocation({
      latitude: Number(info.lat), // 纬度，范围为-90~90，负数表示南纬
      longitude: Number(info.lon), // 经度，范围为-180~180，负数表示西经
      scale: 28, // 缩放比例
      // name: 'name', // 位置名
      // address: 'address', // 地址的详细说明
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  bindCall(e) {
    wx.makePhoneCall({
      phoneNumber: base.phone // 仅为示例，并非真实的电话号码
    })
  },
  bindBackHome() {
    const index = this.data.tabIndex
    wx.navigateTo({
      url: `/pages/index/index?index=${index}`
    })
  },
  bindCollect() {
    const {
      collectFlag
    } = this.data
    collectFlag ? this.deleteCollect() : this.getCollect()
  },
  getCollect() {
    const inforid = this.data.id
    const infortype = Number(this.data.tabIndex) + 1
    app.$api.collect.detailCollect({
      inforid,
      infortype: infortype.toString()
    }).then((res) => {
      const data = res.data
      this.setData({
        collectFlag: true,
        collectid: data.id
      })
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        duration: 1000
      })
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  deleteCollect() {
    const id = this.data.collectid
    console.log()
    app.$api.collect.collectDelete({
      id,
      infortype: Number(this.data.tabIndex) + 1
    }).then((res) => {
      console.log(res)
      this.setData({
        collectFlag: false
      })
      wx.showToast({
        title: '取消收藏成功',
        icon: 'success',
        duration: 1000
      })
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  }
})